/*
* Copyright (C) Microsoft. All rights reserved.  
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not 
* use this file except in compliance with the License.  You may obtain a copy 
* of the License at http://www.apache.org/licenses/LICENSE-2.0  
* 
* THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED 
* WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, 
* MERCHANTABLITY OR NON-INFRINGEMENT. 
*
* See the Apache License, Version 2.0 for specific language governing 
* permissions and limitations under the License.
*
*/
#pragma once

#include <string>
#include <vector>
#include <memory>
#include <baja/db/db.h>
#include <journal/models/db/table_interfaces.h>

namespace journal { namespace model { namespace db
{

class page_table : public ipage_table,
                   public icommon_table_client
{
public:
    page_table();
    static std::shared_ptr<ipage_table> create(std::shared_ptr<s::iengine> dbEngine);

    // icommon_table_client
    void defineColumns(std::shared_ptr<s::itable> table);
    void defineIndexColumns(std::shared_ptr<s::iindex> primaryKeyIndex);
    void defineRowCells(std::shared_ptr<std::vector<std::shared_ptr<s::icell>>> row);

    // ibase_table
    std::shared_ptr<icommon_table> common();

    // ipage_table
    void readIds(
        std::vector<b::uint32>& ids);

    void insert(
        const b::uint32& pageId,
        const b::Boolean& linkedData,
        const b::Boolean& thumbnailUpToDate,
        const b::uint32& pagePaperType);

    void updateThumbnail(
        const b::uint32 pageId,
        const b::Boolean& thumbnailUpToDate,
        const s::binary_blob& inBlob);

    void load(
        const b::uint32& pageId,
        b::Boolean& pageLinkedData,
        b::uint32& pagePaperType,
        b::Boolean& pageThumbnailUpToDate,
        std::vector<b::uint32>& pagePhotoIds);

    void loadThumbnail(
        const b::uint32 pageId,
        b::Boolean& pageThumbnailUpToDate,
        s::binary_blob& outBlob);

    void dispose(
        b::uint32 pageId);

    void updateLinkedData(
        const b::uint32 pageId,
        const b::Boolean& linkedData);

    void updatePagePaperType(
        const b::uint32& pageId,
        const b::uint32& pagePaperType);

    void updateThumbnailStatus(
        const b::uint32& pageId,
        const b::Boolean& thumbnailUpToDate);

    void updatePhotoIds(
        const b::uint32 pageId,
        const std::vector<b::uint32>& pageIds);

private:
    std::recursive_mutex _mutex;

    std::shared_ptr<icommon_table> _commonTable;
    std::weak_ptr<icommon_table_client> _commonTableClient;

    std::shared_ptr<s::icolumn_typed<b::uint32>> _pageIdColumn;
    std::shared_ptr<s::icolumn_typed<b::uint32>> _pageLinkedDataColumn;
    std::shared_ptr<s::icolumn_typed<b::uint32>> _pagePaperTypeColumn;
    std::shared_ptr<s::icolumn_typed<b::uint32>> _pageThumbnailUpToDateColumn;
    std::shared_ptr<s::icolumn_typed<b::uint32>> _pageThumbnailSizeColumn;
    std::shared_ptr<s::icolumn_typed<s::binary_blob>> _pageThumbnailColumn;
    std::shared_ptr<s::icolumn_typed<b::uint32>> _pagePhotoIdsSizeColumn;
    std::shared_ptr<s::icolumn_typed<s::binary_blob>> _pagePhotoIdsColumn;

    std::shared_ptr<s::icell_typed<b::uint32>> _pageIdCell;
    std::shared_ptr<s::icell_typed<b::uint32>> _pageLinkedDataCell;
    std::shared_ptr<s::icell_typed<b::uint32>> _pagePaperTypeCell;
    std::shared_ptr<s::icell_typed<b::uint32>> _pageThumbnailUpToDateCell;
    std::shared_ptr<s::icell_typed<b::uint32>> _pageThumbnailSizeCell;
    std::shared_ptr<s::icell_typed<s::binary_blob>> _pageThumbnailCell;
    std::shared_ptr<s::icell_typed<b::uint32>> _pagePhotoIdsSizeCell;
    std::shared_ptr<s::icell_typed<s::binary_blob>> _pagePhotoIdsCell;

    std::shared_ptr<s::iindex_column> _pageIdIndexColumn;

    std::shared_ptr<std::vector<std::shared_ptr<s::icell>>> _thumbnailCells;

    std::shared_ptr<std::vector<std::shared_ptr<s::icell>>> _pagePaperTypeCells;

    std::shared_ptr<std::vector<std::shared_ptr<s::icell>>> _pageLinkedDataCells;

    std::shared_ptr<std::vector<std::shared_ptr<s::icell>>> _pageThumbnailUpToDateCells;

    std::shared_ptr<std::vector<std::shared_ptr<s::icell>>> _pagePhotoIdsCells;

    // Helper methods
    void read(b::uint32 pageId,
              std::shared_ptr<s::iresult> result);

    void readThumbnail(
        const b::uint32 pageId,
        std::shared_ptr<s::iresult> result);
};

std::shared_ptr<ipage_table> createPageTable(std::shared_ptr<s::iengine> dbEngine);

}}} // namespace journal::model::db